package com.ruoyi.classai.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.classai.domain.EwcChapter;
import com.ruoyi.classai.domain.EwcClass;
import com.ruoyi.classai.service.IEwcClassService;
import com.ruoyi.classai.service.IEwcStudentService;
import com.ruoyi.common.core.context.ThirdSessionHolder;
import com.ruoyi.classai.domain.EwcCourseplan;
import com.ruoyi.classai.domain.EwcCourseplanTeaching;
import com.ruoyi.classai.domain.dto.ModuleLessonPreparationModificationDto;
import com.ruoyi.classai.domain.dto.courseplan.EwcCourseplanDto;
import com.ruoyi.classai.domain.dto.courseplan.EwcCourseplanTeachingDto;
import com.ruoyi.classai.domain.vo.courseplan.EwcCourseplanVo;
import com.ruoyi.classai.mapper.EwcCourseplanMapper;
import com.ruoyi.classai.service.IEwcCourseplanService;
import com.ruoyi.classai.service.IEwcCourseplanTeachingService;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class EwcCourseplanServiceImpl extends ServiceImpl<EwcCourseplanMapper, EwcCourseplan> implements IEwcCourseplanService {

    @Autowired
    private EwcCourseplanMapper ewcCourseplanMapper;
    @Autowired
    private IEwcCourseplanTeachingService courseplanTeachingService;
    @Autowired
    private IEwcStudentService studentService;

    @Override
    public EwcCourseplan basicInformationLessonScheduleInterface(ModuleLessonPreparationModificationDto moduleLessonPreparationModificationDto) {
        moduleLessonPreparationModificationDto.setTeacherId(ThirdSessionHolder.getThirdSession().getId());
        List<EwcCourseplan> ewcCourseplan= ewcCourseplanMapper.getBasicInformationLessonSchedule(moduleLessonPreparationModificationDto);

       String targetTeacherId=ThirdSessionHolder.getThirdSession().getId();
        if(ewcCourseplan.size()>1){
           for (EwcCourseplan courseplan : ewcCourseplan) {
               if (courseplan.getTeacherId().equals(targetTeacherId)) {
                   courseplan.setTeacherId(ThirdSessionHolder.getThirdSession().getId());
                   courseplan.setTeacherName(ThirdSessionHolder.getThirdSession().getTeacherName());
                   return courseplan;
               }
           }
       }
        ewcCourseplan.get(0).setTeacherId(ThirdSessionHolder.getThirdSession().getId());
        ewcCourseplan.get(0).setTeacherName(ThirdSessionHolder.getThirdSession().getTeacherName());
        //统计男女生数量
        int boy=studentService.getStudentNumberBySex(moduleLessonPreparationModificationDto.getClassId(),"男");
        int girl=studentService.getStudentNumberBySex(moduleLessonPreparationModificationDto.getClassId(),"女");
        if (boy==0&&girl>0){ //
            ewcCourseplan.get(0).setClassType("3");//女生班
        } else if (girl==0&&boy>0) {
            ewcCourseplan.get(0).setClassType("2");//男生班
        }else {
            ewcCourseplan.get(0).setClassType("1");//自然班
        }
        return ewcCourseplan.get(0);
    }

    @Override
    public Boolean saveEwcCourseplan(EwcCourseplan ewcCourseplan) {
        ewcCourseplan.setTeacherId(ThirdSessionHolder.getThirdSession().getId());
        ewcCourseplan.setTeacherName(ThirdSessionHolder.getThirdSession().getTeacherName());
        return this.save(ewcCourseplan);
    }

    @Override
    public long queryCourseplan(EwcCourseplan ewcCourseplan) {
        QueryWrapper<EwcCourseplan> queryWrapper=new QueryWrapper<EwcCourseplan>();
        queryWrapper.eq("chapter_id",ewcCourseplan.getChapterId());
        queryWrapper.eq("teacher_id",ThirdSessionHolder.getThirdSession().getId());
        queryWrapper.eq("course_id",ewcCourseplan.getCourseId());
        queryWrapper.eq("class_id",ewcCourseplan.getClassId());
        long count = this.count(queryWrapper);
        return count;
    }

    @Override
    public void updateCourseplan(EwcCourseplan ewcCourseplan) {
        UpdateWrapper<EwcCourseplan> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("chapter_id",ewcCourseplan.getChapterId());
        updateWrapper.eq("teacher_id",ThirdSessionHolder.getThirdSession().getId());
        updateWrapper.eq("course_id",ewcCourseplan.getCourseId());
        updateWrapper.eq("class_id",ewcCourseplan.getClassId());
        this.update(ewcCourseplan, updateWrapper);
    }

    @Override
    public EwcCourseplanVo getLessonPlanById(String coursePlanId) {
        //查询课程计划
        EwcCourseplan ewcCourseplan=this.getById(coursePlanId);
        if (Objects.isNull(ewcCourseplan)){
            throw new GlobalException("没有查询到课程计划信息");
        }
        EwcCourseplanVo courseplanVo=new EwcCourseplanVo();
        BeanUtils.copyProperties(ewcCourseplan,courseplanVo);
        courseplanVo.setCoursePlanId(coursePlanId);
        //查询教学部分内容
        List<EwcCourseplanTeaching> courseplanTeachings=courseplanTeachingService.list(Wrappers.<EwcCourseplanTeaching>lambdaQuery()
                .eq(EwcCourseplanTeaching::getCoursePlanId,coursePlanId).orderByAsc(EwcCourseplanTeaching::getSort));
        if (CollectionUtil.isNotEmpty(courseplanTeachings)){
            courseplanVo.setCourseplanTeachings(courseplanTeachings);
        }
        return courseplanVo;
    }

    @Override
    public void addOrEditLessonPlan(EwcCourseplanDto courseplanDto) {
        //更新课程计划
        EwcCourseplan ewcCourseplan=new EwcCourseplan();
        ewcCourseplan.setId(courseplanDto.getCoursePlanId());
        ewcCourseplan.setResourceSupport(courseplanDto.getResourceSupport());
        ewcCourseplan.setSecurityGuarantee(courseplanDto.getSecurityGuarantee());
        ewcCourseplan.setFullCourse(courseplanDto.getFullCourse());
        ewcCourseplan.setContentTheme(courseplanDto.getContentTheme());
        ewcCourseplan.setPracticeIntensity(courseplanDto.getPracticeIntensity());
        ewcCourseplan.setClassroomReflection(courseplanDto.getClassroomReflection());
        this.updateById(ewcCourseplan);
        //添加课程规划
        List<EwcCourseplanTeachingDto> teachingDtos=courseplanDto.getCourseplanTeachingDtos();
        for (EwcCourseplanTeachingDto teachingDto : teachingDtos) {
            EwcCourseplanTeaching courseplanTeaching=new EwcCourseplanTeaching();
            BeanUtils.copyProperties(teachingDto,courseplanTeaching);
            courseplanTeachingService.saveOrUpdate(courseplanTeaching);
        }
    }

    @Override
    public void updateReflection(EwcChapter ewcChapter, String aiResponse) {
        EwcCourseplan ewcCourseplan = new EwcCourseplan();
        UpdateWrapper<EwcCourseplan> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("chapter_id",ewcChapter.getId());
        updateWrapper.eq("course_id",ewcChapter.getCourseId());
        updateWrapper.eq("class_id",ewcChapter.getClassId());
        ewcCourseplan.setClassroomReflection(aiResponse);
        this.update(ewcCourseplan, updateWrapper);
    }
}
